package com.hwtx.form.domain.service;

import com.google.common.collect.Lists;
import com.hwtx.form.annotation.FormValidation;
import com.hwtx.form.domain.def.FormDef;
import com.hwtx.form.domain.def.FormValidate;
import com.hwtx.form.domain.def.FormValidateDef;

import java.util.List;

@FormValidation(formType = DbOrFieldNameValidate.FORM_TYPE, key = {"name"})
public class DbOrFieldNameValidate implements FormValidate {
    static final String FORM_TYPE = "formConfig";
    static final List<String> DEFAULT_FORM_FIELD = Lists.newArrayList("show", "create", "table");

    @Override
    public FormValidateDef.ValidationResult validate(FormDef formDef, String name, Object value) {
        if (formDef.getName().endsWith("form")) {
            if (DEFAULT_FORM_FIELD.contains(value.toString())) {
                return FormValidateDef.ValidationResult.builder().pass(false).message("数据库保留关键字不可用").key(name).build();
            }
        }
        boolean allNumber = value.toString().matches("^\\d+$");
        if (!allNumber) {
            return FormValidateDef.ValidationResult.builder().pass(true).build();
        } else {
            return FormValidateDef.ValidationResult.builder().pass(false).message("请输入非全数字的名称").key(name).build();
        }
    }
}
